/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import Gmail.GuiEmail;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author Envy
 */
public class Send extends javax.swing.JFrame {

    /**
     * Creates new form Send
     */
    public Send() {
        initComponents();
        chkFileAttach.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtTieuDe = new javax.swing.JTextField();
        txtNguoiNhan = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtNoiDung = new javax.swing.JTextArea();
        btnSend = new javax.swing.JButton();
        btnFileAttach = new javax.swing.JButton();
        chkFileAttach = new javax.swing.JCheckBox();
        lbFileName = new javax.swing.JLabel();
        btnMaHoaVaGui = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Soạn Email");

        jLabel1.setText("Tiêu đề : ");

        txtTieuDe.setText("Test");

        txtNguoiNhan.setText("jbkhoi90@gmail.com, thanh19842013@gmail.com");

        jLabel2.setText("Người nhận : ");

        jLabel5.setText("Nội dung :");

        txtNoiDung.setColumns(20);
        txtNoiDung.setRows(5);
        txtNoiDung.setText("Test ...\nTest....");
        jScrollPane1.setViewportView(txtNoiDung);

        btnSend.setText("Gửi");
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });

        btnFileAttach.setText("Attach...");
        btnFileAttach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFileAttachActionPerformed(evt);
            }
        });

        chkFileAttach.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                chkFileAttachStateChanged(evt);
            }
        });

        btnMaHoaVaGui.setText("Mã hóa và gửi");
        btnMaHoaVaGui.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMaHoaVaGuiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel5))
                        .addGap(46, 46, 46)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtNguoiNhan, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
                            .addComponent(txtTieuDe))
                        .addContainerGap(278, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 705, Short.MAX_VALUE)
                        .addGap(123, 123, 123))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnSend, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                            .addComponent(btnFileAttach, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(68, 68, 68)
                                .addComponent(chkFileAttach)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 652, Short.MAX_VALUE)
                                .addComponent(lbFileName))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnMaHoaVaGui)
                                .addGap(0, 0, Short.MAX_VALUE))))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtTieuDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNguoiNhan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(jLabel5)
                .addGap(15, 15, 15)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnFileAttach)
                            .addComponent(chkFileAttach))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnSend)
                            .addComponent(btnMaHoaVaGui))
                        .addGap(37, 37, 37))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbFileName)
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    public static String nguoinhan;
    public static String tieude;
    public static String noidung;
    public static String keygen = null;
    public static File fileAttach = null,
            fileTam = null;    
    
    
    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
        // TODO add your handling code here:
        LoadInfo();
        GuiEmail gui = new GuiEmail();
        gui.sendGmail(nguoinhan, tieude, noidung, fileAttach);

    }//GEN-LAST:event_btnSendActionPerformed

    public void LoadInfo(){
    
        nguoinhan = getTxtNguoiNhan().getText();
        tieude = getTxtTieuDe().getText();
        noidung = getTxtNoiDung().getText();

        
    }
    private void btnFileAttachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFileAttachActionPerformed
        // TODO add your handling code here:
        JFileChooser jf = new JFileChooser();

        jf.setCurrentDirectory(new File("D:\\"));
        //FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
        //jf.setFileFilter(filter);
        int returnVal = jf.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            fileAttach = jf.getSelectedFile().getAbsoluteFile();
            fileTam = fileAttach;
            HienThiFile();
        }
    }//GEN-LAST:event_btnFileAttachActionPerformed

    private void chkFileAttachStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_chkFileAttachStateChanged
        // TODO add your handling code here:
        if(chkFileAttach.isSelected()){
            fileAttach = fileTam;
        }
        else
            fileAttach = null;
    }//GEN-LAST:event_chkFileAttachStateChanged

    private void btnMaHoaVaGuiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMaHoaVaGuiActionPerformed
        // TODO add your handling code here:
        KhoaBiMat kbm = new KhoaBiMat();
        kbm.setVisible(true);
    }//GEN-LAST:event_btnMaHoaVaGuiActionPerformed
    
    public JTextField getTxtNguoiNhan() {
        return txtNguoiNhan;
    }

    public void setTxtNguoiNhan(JTextField txtNguoiNhan) {
        this.txtNguoiNhan = txtNguoiNhan;
    }

    public JTextArea getTxtNoiDung() {
        return txtNoiDung;
    }

    public void setTxtNoiDung(JTextArea txtNoiDung) {
        this.txtNoiDung = txtNoiDung;
    }

    public JTextField getTxtTieuDe() {
        return txtTieuDe;
    }

    public void setTxtTieuDe(JTextField txtTieuDe) {
        this.txtTieuDe = txtTieuDe;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Send.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Send.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Send.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Send.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Send().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFileAttach;
    private javax.swing.JButton btnMaHoaVaGui;
    private javax.swing.JButton btnSend;
    private javax.swing.JCheckBox chkFileAttach;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbFileName;
    private javax.swing.JTextField txtNguoiNhan;
    public static javax.swing.JTextArea txtNoiDung;
    private javax.swing.JTextField txtTieuDe;
    // End of variables declaration//GEN-END:variables

    private int HienThiFile() {
        if (fileAttach != null) {
            lbFileName.setText(fileAttach.getName());
            chkFileAttach.setSelected(true);
            lbFileName.setVisible(true);
            chkFileAttach.setVisible(true);
            return 1;

        } else {
            lbFileName.setVisible(false);
            chkFileAttach.setVisible(false);
            return 0;
        }
    }
}
